// 提供登录功能，缺乏：
// 1.通过浏览器保存的JWT，直接进入首页
// 2.获得账号、密码，进行登录     --- 完成
// 3.登录跳转界面   ---半完成【如果把数据传给新界面?】
// 4.将返回的JWT，存储到本地浏览器 ---完成
var btn = document.getElementById("login");
var phoneData = document.getElementById("phone");
var pwdData = document.getElementById("pwd");
var code = document.getElementById("code");

btn.onclick = function(){
    // var ten = document.getElementById("he");
    var requestBody = new FormData();
    requestBody.append('username', phoneData.value);
    requestBody.append('password', pwdData.value);
    let object = {};

    requestBody.forEach((value, key) => (object[key] = value));
    var jsonData = JSON.stringify(object);
    
    // options.body = JSON.stringify(body);
    
    // fetch("http://1.94.147.186:8000/fdData/listAll", options).then(response => {
    fetch("http://127.0.0.1:8000/employee/login", 
    {
        headers: {
            'content-Type': 'application/json',
        },
        method:'post',
        body:jsonData
    }).then(response => {   
        return response.json();
    })
    .then(data => {
        //保存信息到本地，里面都token
        localStorage.setItem("project_b526_JWT",JSON.stringify(data.jwt));
        window.location.href = "/index.html";
        // showData(data);
    });

    // 展示数据,这是反馈数据的表单
    function showData(data) {
        var tb = document.getElementById("table_body");
        tb.innerHTML="";
        for (var i = 0; i < data.length; i++) {
            const row = document.createElement('tr');
            const date = new Date(data[i].fdTime);
            const formattedDate = date.toLocaleString(); // 使用本地格式
            row.innerHTML = `
                <td>${data[i].fdId}</td>
                <td>${data[i].levelName}</td>
                <td>${data[i].publicName}</td>
                <td>${data[i].provinceName}</td>
                <td>${data[i].cityName}</td>
                <td>${formattedDate}</td>
                <td>${data[i].fdDescribe}</td>
                <td>${data[i].fdAddress}</td>
                <td>${data[i].fdState}</td>
            `;
            tb.appendChild(row);
        };
        // Toast({
        //     message: "加入购物车成功",
        //     position: 'center',
        //     duration: 1000
        // });
    }
}

code.onclick = function(){
    window.location.href = "/logins/codeLogin.html";
}